/* $Id: WidgetIOUnitCase.java 52 2010-01-11 01:42:30Z aaron.day $ */
package org.crazydays.gameplan.widget;


import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import org.junit.*;
import static org.junit.Assert.*;

import org.crazydays.junit.TestHelper;

/**
 * WidgetIOTestCase
 */
public class WidgetIOUnitCase
{
    /** test helper */
    protected TestHelper testHelper = new TestHelper(WidgetIOUnitCase.class);

    /**
     * Cleanup test helper.
     */
    @After
    public void tearDown()
    {
        testHelper.tearDown();
    }

    /**
     * Test storeCategories(Set<WidgetCategory>,File) and loadCategories(File).
     * 
     * @throws IOException
     */
    @Test
    public void testStoreAndLoadCategories()
        throws IOException
    {
        // objects
        Set<WidgetCategory> categories = new TreeSet<WidgetCategory>();
        categories.add(new WidgetCategory("one", "set one"));
        categories.add(new WidgetCategory("two", "set two"));

        File destination = new File(testHelper.getSandbox(), "categories.xml");

        // test
        WidgetIO.storeCategories(categories, destination);
        Set<WidgetCategory> result = WidgetIO.loadCategories(destination);

        // verify
        WidgetCategory category;
        assertNotNull("result == null", result);
        Iterator<WidgetCategory> iterator = result.iterator();
        assertTrue("iterator.next", iterator.hasNext());
        category = iterator.next();
        assertEquals("widgetCategory", new WidgetCategory("one", "set one"),
            category);
        assertTrue("iterator.next", iterator.hasNext());
        category = iterator.next();
        assertEquals("widgetCategory", new WidgetCategory("two", "set two"),
            category);

    }
}
